package com.jeequan.jeepay.mgr.ctrl.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.entity.MchPayRoute;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.model.ApiPageRes;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MchPayRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author wxj
 * 2024/10/25
 */
@Api(tags = "商户支付路由管理")
@RestController
@RequestMapping("/api/mchRoute")
public class MchPayRouteConfigController extends CommonCtrl {

    @Autowired
    private MchPayRouteService mchPayRouteService;

    @ApiOperation("查询路由列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "pageNumber", value = "分页页码", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "appId", value = "应用ID"),
            @ApiImplicitParam(name = "wayCode", value = "支付方式"),
            @ApiImplicitParam(name = "state", value = "状态: 0-停用, 1-启用", dataType = "Byte")
    })
    @PreAuthorize("hasAuthority('ENT_MCH_ROUTE_LIST')")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ApiPageRes<MchPayRoute> list() {
        MchPayRoute mchPayRoute = getObject(MchPayRoute.class);
        IPage<MchPayRoute> pages = mchPayRouteService.selectPage(getIPage(), mchPayRoute);
        return ApiPageRes.pages(pages);
    }

    @ApiOperation("新建路由")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "appName", value = "应用名称", required = true),
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true),
            @ApiImplicitParam(name = "wayCode", value = "支付方式", required = true),
            @ApiImplicitParam(name = "remark", value = "备注"),
            @ApiImplicitParam(name = "state", value = "状态: 0-停用, 1-启用", dataType = "Byte"),
            @ApiImplicitParam(name = "strategy", value = "路由策略", required = true),
            @ApiImplicitParam(name = "strategyParams", value = "路由策略参数", required = true)
    })
    @PreAuthorize("hasAuthority('ENT_MCH_ROUTE_ADD')")
    @MethodLog(remark = "新建路由")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ApiRes add() {
        MchPayRoute mchPayRoute = getObject(MchPayRoute.class);
        // 当前登录用户信息
        SysUser sysUser = getCurrentUser().getSysUser();
        mchPayRoute.setCreatedUid(sysUser.getSysUserId());
        mchPayRoute.setCreatedBy(sysUser.getRealname());

        mchPayRouteService.save(mchPayRoute);
        return ApiRes.ok();
    }

    @ApiOperation("路由详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "routeId", value = "路由ID", required = true)
    })
    @PreAuthorize("hasAnyAuthority('ENT_MCH_ROUTE_VIEW', 'ENT_MCH_ROUTE_EDIT')")
    @GetMapping("/{routeId}")
    public ApiRes detail(@PathVariable("routeId") Long routeId) {
        MchPayRoute mchPayRoute = mchPayRouteService.getById(routeId);
        if (mchPayRoute == null) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SELETE);
        }

        return ApiRes.ok(mchPayRoute);
    }

    @ApiOperation("更新路由")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "routeId", value = "路由ID", required = true),
            @ApiImplicitParam(name = "appName", value = "应用名称", required = true),
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true),
            @ApiImplicitParam(name = "wayCode", value = "支付方式", required = true),
            @ApiImplicitParam(name = "remark", value = "备注"),
            @ApiImplicitParam(name = "state", value = "状态: 0-停用, 1-启用", dataType = "Byte"),
            @ApiImplicitParam(name = "strategy", value = "路由策略", required = true),
            @ApiImplicitParam(name = "strategyParams", value = "路由策略参数", required = true)
    })
    @PreAuthorize("hasAuthority('ENT_MCH_ROUTE_EDIT')")
    @MethodLog(remark = "更新路由")
    @PutMapping("/{routeId}")
    public ApiRes update(@PathVariable("routeId") Long routeId) {
        MchPayRoute mchPayRoute = getObject(MchPayRoute.class);
        mchPayRoute.setId(routeId);
        boolean result = mchPayRouteService.updateById(mchPayRoute);
        if (!result) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
        }
        return ApiRes.ok();
    }

    @ApiOperation("删除路由")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "routeId", value = "路由ID", required = true)
    })
    @PreAuthorize("hasAuthority('ENT_MCH_ROUTE_DEL')")
    @MethodLog(remark = "删除路由")
    @DeleteMapping("/{routeId}")
    public ApiRes delete(@PathVariable("routeId") Long routeId) {
        mchPayRouteService.removeById(routeId);
        return ApiRes.ok();
    }
}
